/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import model.ModelListaArticulo;
import tiendagui.internal.JDialogListaArticulo;
import controller.utils.ControllerTable;
import entities.vo.ArticuloVO;

/**
 *
 * @author Jhonny
 */
public class ControllerListaArticulos extends Controller implements ControllerTable{

    private JDialogListaArticulo    dialog;
    private ModelListaArticulo      model;
    private ArticuloVO              articulo;
    public ControllerListaArticulos(JDialogListaArticulo dialog, ModelListaArticulo model) {
        this.dialog = dialog;
        this.model = model;
    }

    @Override
    public void cerrar() {
        dialog.setVisible(false);
    }

    public ArticuloVO getArticulo() {
        return articulo;
    }
    


    

    @Override
    public void initComponentView() throws FileNotFoundException, IOException {
        List<ArticuloVO> list = model.getArticulos();        
        initDatosTablaArticulos(dialog.getTable(), list);
        articulo = null;
        dialog.setBuscar("");
    }

    

    @Override
    public ArticuloVO getArticulo(Integer referencia) {
        ArticuloVO articulo =null;
        if(referencia!=null){
            articulo = model.getArticulo(referencia);
        }
        return articulo;
    }

    public void agregarArticulo() {
        int row = dialog.getTable().getSelectedRow();
        if(row >=0){
            articulo = getArticulo((Integer)((DefaultTableModel)dialog.getTable().getModel()).getValueAt(row, 0));
            dialog.setVisible(false);
            
        }else{
            JOptionPane.showMessageDialog(null, "Debe seleccionar una fila de la tabla.");
        }
    }

	@Override
	public void postAgregar() {
	}

	@Override
	public void postEliminar() {
	}

	public void buscar() {
		List<ArticuloVO> list = new LinkedList<ArticuloVO>();
		Integer referencia = dialog.getBuscar();
		if(referencia!=null){
			ArticuloVO articulo = model.getArticulo(referencia);
			if(articulo!=null){
				list.add(articulo);
			}
		}else{
			list = model.getArticulos();
		}
        initDatosTablaArticulos(dialog.getTable(), list);
		
	}

}
